﻿@model WebSite.Admin.Models.Account.LoginViewModel
<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
    <meta charset="utf-8">
    <title>登录</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- CSS -->
    <link rel="stylesheet" href="/Content/reset.css">
    <link rel="stylesheet" href="/Content/supersized.css">
    <link rel="stylesheet" href="/Content/style.css">
    <link href="~/Scripts/icheck/skins/square_all.css" rel="stylesheet" />
    <link href="~/Scripts/layer/skin/default/layer.css" rel="stylesheet" />
    <link href="~/Scripts/toastr/toastr.min.css" rel="stylesheet" />
    <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
    <!--[if lt IE 9]>
    <script src="~/Scripts/html5.js"></script>
        <![endif]-->
</head>
<body class="pace-done">
    <div class="page-container">
        <form id="loginForm">
            <div class="login-form">
                <p class="logo">
                    用户登录<small> UserLogin</small>
                </p>
                <input type="text" name="UserName" id="username" placeholder="用户名">
                <input type="password" name="Password" id="password" placeholder="密码">
                <p>
                    <input type="checkbox" id="rememberme" name="RememberMe" /><label for="rememberme">记住密码</label>
                </p>
                <button type="submit">登 录</button>
                <div class="error">
                    <span>+</span>
                </div>
            </div>
        </form>
    </div>
</body>
</html>
<!-- Javascript -->
<script src="~/Scripts/jquery.min-1.9.1.js"></script>
<script src="~/Scripts/icheck/icheck.min.js"></script>
<script src="~/Scripts/supersized.3.2.7.min.js"></script>
<script src="~/Scripts/supersized-init.js"></script>
<script src="~/Scripts/layer/layer.js"></script>
<script src="~/Scripts/toastr/toastr.min.js"></script>
<script src="~/Scripts/jquery.extend.js"></script>
<script src="~/Scripts/jquery.cookie.js"></script>
<script>
    $(function () {
        var loginMethod = {
            validate: function (data) {
                var $this = $('.page-container form');
                if (data.UserName === '') {
                    $.msgError('用户名不能为空！');
                    $this.parent().find('#username').focus();
                    return false;
                }
                if (data.Password === '') {
                    $.msgError('密码不能为空！');
                    $this.parent().find('#password').focus();
                    return false;
                }
                return true;
            },
            login: function () {
                $('.page-container form').submit(function () {
                    var userName = $('#username').val();
                    var password = $('#password').val();
                    var rememberMe = $('#rememberme').is(':checked') ? true : false;
                    var data = { UserName: userName, Password: password, RememberMe: rememberMe };
                    if (!loginMethod.validate(data)) return false;
                    var url = '/Account/Login?returnUrl=@Model.ReturnUrl';
                    var success = function (res) {
                        if (res.status === 'success') {
                            loginMethod.rememberPwd();
                            window.location.href = '@Url.Action("Index","Home")';
                        } else {
                            var msg = res.message || '系统错误！';
                            $.msgError(msg);
                        }
                    }
                    $.ajaxPost(url, data, success);
                    return false;
                });
            },
            showPwd: function () {
                $('#username, #password').keyup(function () {
                    $(this).parent().find('.error').fadeOut('fast');
                });
            },
            icheck: function () {
                $.iCheck('#rememberme');
            },
            rememberPwd: function () {
                if ($("#rememberme").prop("checked")) {
                    var username = $("#username").val();
                    var password = $("#password").val();
                    //存储一个带30天期限的cookie
                    $.cookie("APPUSER_COOKIE", "true", { expires: 30 }); 
                    $.cookie("USERNAME", username, { expires: 30 });
                    $.cookie("PASSWORD", password, { expires: 30 });
                }
                else {
                    $.cookie("APPUSER_COOKIE", "false", { expire: -1 });
                    $.cookie("USERNAME", "", { expires: -1 });
                    $.cookie("PASSWORD", "", { expires: -1 });
                }
                return true;
            },
            load: function () {
                if ($.cookie("APPUSER_COOKIE")) {
                    $("#rememberme").prop("checked", true);
                    $("#username").val($.cookie("USERNAME"));
                    $("#password").val($.cookie("PASSWORD"));
                }
            },
            init: function () {
                loginMethod.load();
                loginMethod.login();
                loginMethod.showPwd();
                loginMethod.icheck();
            }
        }

        loginMethod.init();
    });
</script>
